package com.biz.primus.ms.coupon.event.listener;

import com.biz.primus.ms.coupon.dao.po.Coupon;
import com.biz.primus.ms.coupon.dao.repository.CouponRepository;
import com.biz.primus.ms.coupon.dao.ro.CouponRo;
import com.biz.primus.ms.coupon.event.coupon.CreateCouponEvent;
import com.biz.primus.ms.coupon.trans.CouponRo2Coupon;
import com.google.common.collect.Lists;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 优惠券事件监听器
 *
 * @author david-liu
 */
@Component
public class CouponEventListener {

    @Autowired
    private CouponRepository couponRepository;

    @EventListener
    @Transactional
    public void handleCreateCouponEvent(CreateCouponEvent event) {
        List<CouponRo> couponRos = event.getCouponRos();
        CouponRo2Coupon couponRo2Coupon = new CouponRo2Coupon(event.getCouponType());
        List<Coupon> coupons = Lists.transform(couponRos, couponRo2Coupon);
        couponRepository.save(coupons);
    }

}
